/**
 * popup.js
 * Handle's hulu popup
 */

loadScript("search-popup.js");

function fetchNewQueueIdFeed()
{
	var dctk = connected.toolkit;
	var queueIdEdit = dctk.getElementById("queueIdEdit");	
	
	queueId = queueIdEdit.text;
	var storage = connected.openStorage(huluStorage,true)
	storage.set(huluQueueId,queueId)
	feed = "http://www.hulu.com/feed/queue/"+queueId;
	feedTitle = "MY HULU FEEDS";
	onLoadComplete();
}

function popupButtonHandler(e)
{
	var dctk = connected.toolkit;

	switch(e.buttonId)
	{
		case dcKeyEvent.BUTTON_ID_BACK:
		{
			dctk.dismissPopup();
		}
		break;

		case dcKeyEvent.BUTTON_ID_SELECT:
		{
			fetchNewQueueIdFeed();
			dctk.dismissPopup();
		}
		break;
		
		case dcKeyEvent.BUTTON_ID_INFO:
		{
			dctk.dismissPopup();
			showInfoPopup();
		}
		break;
	}
}

function showQueueIdPopup()
{
	var dctk = connected.toolkit;
	
	// create a new panel for the popup
	var popup = dctk.createPanelCtrl("popup", true);
	popup.width = 240;
	popup.height = 190;
	popup.outlined = true;
	popup.filled = true;
	popup.color.rgb = 0x000000;
	popup.color.a = 192;
	popup.onUnhandledKeyEvent = popupButtonHandler;
	
	var buttonFont = 22;
	
	var queueHeadTxt = dctk.createLabelCtrl("queueHeadTxt", true);
	queueHeadTxt.setRect(0, 164, 240, 184);
	queueHeadTxt.textColor.rgb = 0xCDCDCD;
	queueHeadTxt.fontSize = 18;
	queueHeadTxt.text = "Manage your queue at Hulu.com";
	queueHeadTxt.align = dctkLabel.TS_CENTER;
	queueHeadTxt.valign = dctkLabel.TS_MIDDLE;
	popup.addChild(queueHeadTxt);
	
	var queueIdTxt = dctk.createLabelCtrl("queueIdTxt", true);
	queueIdTxt.setRect(8, 135, 100, 160);
	queueIdTxt.textColor.rgb = 0xCDCDCD;
	queueIdTxt.fontSize = buttonFont;
	queueIdTxt.text = "Queue ID:";
	queueIdTxt.align = dctkLabel.TS_RIGHT;
	queueIdTxt.valign = dctkLabel.TS_MIDDLE;
	popup.addChild(queueIdTxt);

	var queueIdEdit = dctk.createEditCtrl("queueIdEdit", true);
	queueIdEdit.setRect(110, 135, 215, 160);
	queueIdEdit.text = queueId;
	queueIdEdit.fontSize = buttonFont;
	queueIdEdit.numeric = true;
	popup.addChild(queueIdEdit);

	var okBtn = dctk.createButtonCtrl("okBtn", true);
	okBtn.setRect(40, 80, 200, 120);
	okBtn.text = "Save Queue ID";
	okBtn.fontSize = buttonFont;
	okBtn.onSelect = function(e) { fetchNewQueueIdFeed(); dctk.dismissPopup(); }
	popup.addChild(okBtn);
	
	var searchBtn = dctk.createButtonCtrl("searchBtn", true);
	searchBtn.setRect(40, 25, 200, 65);
	searchBtn.text = "Search";
	searchBtn.fontSize = buttonFont;
	searchBtn.onSelect = function(e) { searchString = ""; dctk.dismissPopup(); showSearchPopup(); }
	popup.addChild(searchBtn);
	
	var queueHelpTxt = dctk.createLabelCtrl("queueHelpTxt", true);
	queueHelpTxt.setRect(0, 0, 240, 20);
	queueHelpTxt.textColor.rgb = 0xCDCDCD;
	queueHelpTxt.fontSize = 14;
	queueHelpTxt.text = "Press INFO for help...";
	queueHelpTxt.align = dctkLabel.TS_CENTER;
	queueHelpTxt.valign = dctkLabel.TS_MIDDLE;
	popup.addChild(queueHelpTxt);
	
	// show the popup
	dctk.invokePopup(popup);
	
	// set the sibling mappings
	queueIdEdit.topSibling = searchBtn;
	queueIdEdit.bottomSibling = okBtn;
	okBtn.topSibling = queueIdEdit;
	okBtn.bottomSibling = searchBtn;
	searchBtn.topSibling = okBtn;
	searchBtn.bottomSibling = queueIdEdit;
	
	// start with the focus on the edit control
	dctk.focusedElement = queueIdEdit;
}

